package com.hooya.fa.eu.biz.dao.manager.special;

import com.hooya.fa.eu.api.bo.stk.BuyRecordBO;
import com.hooya.fa.eu.api.bo.stk.HistoryBelongsBO;
import com.hooya.fa.eu.api.util.DateExtUtil;
import com.hooya.fa.eu.biz.dao.mapper.TempMapper;
import com.hooya.fa.eu.biz.service.TempRelationService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description 历史进仓货权Manager
 * @Author : Zongz
 * @Copyright Copyright (c) 豪雅集团
 * @Create 2024/2/27
 * @Version 1.0.0
 */
@Slf4j
@Component
@AllArgsConstructor
public class JcHisBelongsManager {

    private final TempRelationService tempRelationService;

	private final TempMapper tempMapper;

    /**
     * 整合数据，修改进仓货权
     *
     * @param recordList 购买记录
     * @return
     */
    public List<HistoryBelongsBO> editJcHisBelongsAndIntegration(List<BuyRecordBO> recordList) {
        List<HistoryBelongsBO> warehousingHisList = new LinkedList<>();
        recordList.forEach(l -> {
            HistoryBelongsBO bo = HistoryBelongsBO.builder().belongs(l.getBelongs()).htrq(l.getHtrq())
                    .cpbh(l.getCpbh()).place(l.getPlace()).type(l.getType()).build();
            warehousingHisList.add(bo);
        });
        //历史进仓转运退货和23年3月后转运数据
        List<HistoryBelongsBO> hisBelongsList = tempRelationService.getHistoryJcZyBelong();  // TODO - 数据源待修改 rxh_europe_result.hq_history   fa_eu.temp_zyjl
        //23年3月后退货数据
        List<HistoryBelongsBO> thBelongsList = tempRelationService.getTempThBelongs();  // TODO - 数据源待修改 fa_eu.temp_thjl  meta_warehouse
        thBelongsList.addAll(warehousingHisList);
        hisBelongsList.addAll(thBelongsList);
        //所有货权信息去重
        return hisBelongsList.stream().distinct().collect(Collectors.toList());
    }

	public List<HistoryBelongsBO> editJcHisBelongsAndIntegrationDaily(List<BuyRecordBO> recordList) {
		List<HistoryBelongsBO> warehousingHisList = new LinkedList<>();
		recordList.forEach(l -> {
			HistoryBelongsBO bo = HistoryBelongsBO.builder().belongs(l.getBelongs()).htrq(l.getHtrq())
					.cpbh(l.getCpbh()).place(l.getPlace()).type(l.getType()).build();
			warehousingHisList.add(bo);
		});
		//历史进仓转运退货和23年3月后转运数据
		List<HistoryBelongsBO> hisBelongsList = tempMapper.getHistoryJcZyBelongDaily();  // TODO - 数据源待修改 rxh_europe_result.hq_history   fa_eu.temp_zyjl
		//23年3月后退货数据
		List<HistoryBelongsBO> thBelongsList = tempMapper.getTempThBelongsDaily();  // TODO - 数据源待修改 fa_eu.temp_thjl  meta_warehouse
		thBelongsList.addAll(warehousingHisList);
		hisBelongsList.addAll(thBelongsList);
		//所有货权信息去重
		return hisBelongsList.stream().distinct().collect(Collectors.toList());
	}

    /**
     * 筛选fba进仓历史获取按数据
     *
     * @param df                日期格式工具
     * @param startDate         开始时间
     * @param allHisBelongsList 所有货权数据
     * @param fbaCkMap
     * @return fba货权数据
     */
    public List<HistoryBelongsBO> getFbaJcHisBelongsData(SimpleDateFormat df, String startDate,
                                                         List<HistoryBelongsBO> allHisBelongsList,
                                                         Map<String, String> fbaCkMap) {
        return allHisBelongsList.stream()
                .filter(l -> {
                    try {
                        long htTime = df.parse(l.getHtrq()).getTime();
                        //获取1年前的时间
                        long beforeYearTime = df.parse(DateExtUtil.getBeforeNMonth(12, startDate)).getTime();
                        if (htTime >= beforeYearTime &&
                                StringUtils.isNotBlank(fbaCkMap.get(l.getPlace()))) {
                            return true;
                        }
                    } catch (Exception e) {
                        throw new RuntimeException(e.getMessage() + ",时间格式化失败！htrq：" + l.getHtrq());
                    }
                    return false;
                })
                .collect(Collectors.toList());
    }
}
